﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Security.Cryptography;
using System.Web;


namespace IHISCETeim.SendSMSSvr
{
    /// <summary>
    /// http://www.shang-yi.com/ 上意官网
    /// </summary>
    public class ShangYiSMS : ISMS
    {
        #region
        /// <summary>
        /// 获取md5码
        /// </summary>
        /// <param name="source">待转换字符串</param>
        /// <returns>md5加密后的字符串</returns>
        private string getMD5(string source)
        {
            string result = "";
            try
            {
                MD5 getmd5 = new MD5CryptoServiceProvider();
                byte[] targetStr = getmd5.ComputeHash(UnicodeEncoding.UTF8.GetBytes(source));
                result = BitConverter.ToString(targetStr).Replace("-", "");
                return result;
            }
            catch (Exception)
            {
                return "0";
            }

        }

        private string getErrorMSG(string errcode)
        {
            //-2	帐号/密码不正确
            //-3	重复登陆
            //-4	余额不足
            //-5	数据格式错误
            //-6	参数有误
            //-7	权限受限
            //-8	流量控制错误
            //-9	扩展码权限错误
            //-10	内容长度长
            //-11	数据库错误
            //-12	序列号状态错误
            string resultmsg = "";
            switch (errcode)
            {
                case "-2":
                    resultmsg = "帐号/密码不正确";
                    break;
                case "-3":
                    resultmsg = "重复登陆";
                    break;
                case "-4":
                    resultmsg = "余额不足";
                    break;
                case "-5":
                    resultmsg = "数据格式错误";
                    break;
                case "-6":
                    resultmsg = "参数有误";
                    break;
                case "-7":
                    resultmsg = "权限受限";
                    break;
                case "-8":
                    resultmsg = "流量控制错误";
                    break;
                case "-9":
                    resultmsg = "扩展码权限错误";
                    break;
                case "-10":
                    resultmsg = "内容长度长";
                    break;
                case "-11":
                    resultmsg = "数据库错误";
                    break;
                case "-12":
                    resultmsg = "序列号状态错误";
                    break;
            }
            return resultmsg + "[代码" + errcode + "]";

        }

        #endregion
        ShangYiWebService.WebService WEBSMS = null;
        string sn = "SDK-ZRG-010-02652", pwd = "099328";
        public ShangYiSMS()
        {
            WEBSMS = new ShangYiWebService.WebService();
            sn = ConfigurationManager.AppSettings["sn"];
            pwd = ConfigurationManager.AppSettings["pwd"];
        }
        /// <summary>
        /// 发送短信
        /// </summary>
        /// <param name="mobile"></param>
        /// <param name="smsmessage"></param>
        /// <returns>
        /// 发送成功，返回"".否则返回失败消息
        /// </returns>
        public string SingSend(string mobile, string smsmessage, string stime)
        {
            string result;
           // WEBSMS.s
            //调用发送短信的发送 还可以调用其他方法 执行其他操作
            smsmessage = HttpUtility.HtmlEncode(smsmessage);
            result = WEBSMS.mt(sn, getMD5(sn + pwd), mobile, smsmessage, "", stime, "");
            if (result.StartsWith("-"))
            {
                return getErrorMSG(result);
            }
            else
            {
                return "";
            }
        }
        /// <summary>
        /// 批量发送
        /// [同一内容发送到不同手机]
        /// </summary>
        /// <param name="mobile">多个手机号用,隔开</param>
        /// <param name="smsmessage"></param>
        /// <param name="stime">定时发送时间,如果有则是时间格式，如果无放空</param>
        /// <returns></returns>
        public string BatchSend(string mobile, string smsmessage, string stime)
        {
            string result;
            //调用发送短信的发送 还可以调用其他方法 执行其他操作
            smsmessage = HttpUtility.HtmlEncode(smsmessage);
            result = WEBSMS.mt(sn, getMD5(sn + pwd), mobile, smsmessage, "", stime, "");
            if (result.StartsWith("-"))
            {
                return getErrorMSG(result);
            }
            else
            {
                return "";
            }
        }
        /// <summary>
        /// 个性批量发送
        /// 一个手机号对应一个短信内容
        /// </summary>
        /// <param name="mobile">多个手机号用,隔开</param>
        /// <param name="smsmessage">多短信内容使用,隔开</param>
        /// <param name="stime">定时发送时间,如果有则是时间格式，如果无放空</param>
        /// <returns></returns>
        public string PersonalitySend(string mobile, string smsmessage, string stime)
        {
            smsmessage = HttpUtility.HtmlEncode(smsmessage);
            int mobilecount = mobile.Split(new char[] { ',' }).Count();
            int smsmessagecount = smsmessage.Split(new char[] { ',' }).Count();
            if (mobilecount != smsmessagecount)
                return "手机号数量与短信数量不一样";
           
            string result;
            //调用发送短信的发送 还可以调用其他方法 执行其他操作
            result = WEBSMS.gxmt(sn, getMD5(sn + pwd), mobile, smsmessage, "", stime, "");
            if (result.StartsWith("-"))
            {
                return getErrorMSG(result);
            }
            else
            {
                return "";
            }
        }

        /// <summary>
        /// 获取余额,
        /// 如果查询成功，返回余额，反之返回失败消息
        /// </summary>
        /// <returns></returns>
        public string GetBalance()
        {
            //ShangYiSMSWS.WebServiceSoapClient WEBSMS = new ShangYiSMSWS.WebServiceSoapClient();
            //string sn = "SDK-ZRG-010-02652", pwd = "099328";
            string result;
            result = WEBSMS.GetBalance(sn, pwd);
            if (result.StartsWith("-"))
            {
                return getErrorMSG(result);
            }
            else
            {
                return result;
            }
        }
    }
}

